﻿using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Text;


namespace Common
{
    public class ConFactory
    {

        public static string MsSqlConStr
        {
            get;set;
        }

        public static IDbConnection GetMsSqlCon(string sqlConnectionString = "")
        {
            if (string.IsNullOrEmpty(sqlConnectionString))
            {
                MsSqlConStr = ConfigurationManager.ConnectionStrings["MsSqlConStr"].ConnectionString;//IIS初始化时，内存中生成一份配置文件的对象，后期读取是从内存中读取，并非再次读文件
            }
            else
            {
                MsSqlConStr = sqlConnectionString;
            }
            SqlConnection conn = new SqlConnection(MsSqlConStr);
            conn.Open();

            return conn;
        }

        public static IDbConnection GetMsSqlConByUidAndPassword(string uid,string password)
        {
            StringBuilder sb = new StringBuilder(ConfigurationManager.ConnectionStrings["MsSqlConStr"].ConnectionString);
            MsSqlConStr=sb.Replace("@UID",uid).Replace("@Password",password).ToString();
            SqlConnection conn = new SqlConnection(MsSqlConStr);
            conn.Open();

            return conn;
        }

    }
}
